//
// Copyright 2023 The Zetyun.GCP Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package response

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

// ResponseBody 响应信息结构体
type ResponseBody struct {
	Duration string      `json:"duration"`
	Data     interface{} `json:"data"`
	Msg      string      `json:"msg"`
	Status   int32       `json:"status"`
}

// TimeCost 计算方法执行耗时
func TimeCost(start time.Time, body *ResponseBody) {
	body.Duration = time.Since(start).String()
}

var (
	SuccessResponse = &ResponseBody{
		Status: 200,
	}
	// Token 认证失败
	ErrJwtVerify = &ResponseBody{
		Status: 135001,
		Msg:    "Token verify error",
	}
	// 权限不足
	ErrAuthorization = &ResponseBody{
		Status: 135002,
		Msg:    "Authorization error",
	}
	// 操作不允许
	ErrAccessDeny = &ResponseBody{
		Status: 135003,
		Msg:    "Access deny",
	}
	// 无效的请求参数
	ErrRequestParams = &ResponseBody{
		Status: 135004,
		Msg:    "Invalid request parameters",
	}
	// 文件路径为空
	ErrEmptyFilePath = &ResponseBody{
		Status: 135005,
		Msg:    "File path is empty",
	}
	// 无效的文件名
	ErrInvalidFileName = &ResponseBody{
		Status: 135006,
		Msg:    "Invalid file name",
	}
	// 文件或文件夹不存在
	ErrFileOrDirNotExist = &ResponseBody{
		Status: 135007,
		Msg:    "No such file or directory",
	}
	// 大文件不支持在线预览，请下载后查看
	ErrReadLargeFile = &ResponseBody{
		Status: 135008,
		Msg:    "Large files do not support preview, please download after reading",
	}
	// 读取文件错误
	ErrReadFile = &ResponseBody{
		Status: 135009,
		Msg:    "Read file error",
	}
	// 创建文件错误
	ErrCreateFile = &ResponseBody{
		Status: 135010,
		Msg:    "Create file error",
	}
	// 上传文件错误
	ErrUploadFile = &ResponseBody{
		Status: 135011,
		Msg:    "Upload file error",
	}
	// 下载文件错误
	ErrDownloadFile = &ResponseBody{
		Status: 135012,
		Msg:    "Download file error",
	}
	// 删除文件错误
	ErrDeleteFile = &ResponseBody{
		Status: 135013,
		Msg:    "Delete file error",
	}
	// 重命名文件错误
	ErrRenameFile = &ResponseBody{
		Status: 135014,
		Msg:    "Rename file error",
	}
	// 写入文件错误
	ErrWriteFile = &ResponseBody{
		Status: 135015,
		Msg:    "Write file error",
	}
	// 文件已存在
	ErrFileNotExist = &ResponseBody{
		Status: 135016,
		Msg:    "File already exist",
	}
	// 创建SSH客户端错误
	ErrCreateSSHClient = &ResponseBody{
		Status: 135017,
		Msg:    "Create SSH client error",
	}
	// 创建Websocket错误
	ErrCreateWebsocket = &ResponseBody{
		Status: 135018,
		Msg:    "Create websocket error",
	}
)

func CustemError(msg string) *ResponseBody {
	return &ResponseBody{
		Msg:    msg,
		Status: 1007,
	}
}

func Response(c *gin.Context, resp ResponseBody, data interface{}) {
	httpStatusCode := http.StatusOK

	c.JSON(httpStatusCode, ResponseBody{
		Status: resp.Status,
		Msg:    resp.Msg,
		Data:   data,
	})

}
